package org.ofbiz.wvtools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class CoSecurityExcelServices {
	public static final String module = CoSecurityExcelServices.class.getName();
    public static final String resource = "WvtoolsUiLabels";
    

	
	/**
	 * 导入系统安全组
	 */
	public static Map<String, Object> importSysSecurityGroup(DispatchContext dctx,
 			Map<String, ? extends Object> context) {
 			Map<String, Object> result = ServiceUtil.returnSuccess();
 	    	Delegator delegator = dctx.getDelegator();
 	    	List<GenericValue> toBeStore = new ArrayList();
 	    	List<String> messageList = new ArrayList<String>();
			List<Map<String,Object>> sheetList = (List<Map<String,Object>>) context.get("sheetList");
			try {
				int i =0;
				for(Map<String,Object> row : sheetList){
					i=i+1;
					String groupId = ExcelPoiUtils.getCellString(row, 0);
					String description = ExcelPoiUtils.getCellString(row, 1);	
					String longDescription = ExcelPoiUtils.getCellString(row, 2);
					String ownerPartyId = ExcelPoiUtils.getCellString(row, 3);
					
					
					//判断
					if(UtilValidate.isEmpty(description)){
						messageList.add("第" + i + "行,权限组名称不能为空。" );
						continue;
					}
					if(UtilValidate.isEmpty(ownerPartyId)){
						messageList.add("第" + i + "行,权限组所属机构不能为空。" );
						continue;
					}
					
					
					
					//创建
					if(UtilValidate.isEmpty(groupId)){
						groupId =delegator.getNextSeqId("SecurityGroup");
					}
        			
					GenericValue entity = delegator.makeValue("SecurityGroup",UtilMisc.toMap("groupId", groupId));
					entity.set("removed", "N");
					entity.set("description", description);
					if(UtilValidate.isNotEmpty(longDescription)){
						entity.set("longDescription", longDescription);
					}
					entity.set("ownerPartyId", ownerPartyId);
					toBeStore.add(entity);
        			
				}
				delegator.storeAll(toBeStore);
				result.put("messageList", messageList);
			} catch (GenericEntityException e) {
				Debug.logError(e.getMessage(), module);
			}
 			return result;
 	}
	
	/**
	 * 导出系统安全组
	 */
	                                  
	public static Map<String, Object> exportSysSecurityGroup(DispatchContext dctx,
			Map<String, ? extends Object> context) {  
//    	java.text.DateFormat orderDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Delegator delegator = dctx.getDelegator();
    	String ids =  (String)context.get("ids");
    	List<String> baseIdList = FastList.newInstance();
    	if(UtilValidate.isNotEmpty(ids)){
    		baseIdList = Arrays.asList(ids.split(";"));
    	}
		Map<String, Object> result = ServiceUtil.returnSuccess();

		EntityCondition mainCond = null;
		List<EntityCondition> andExprs = FastList.newInstance();
    	
		if(UtilValidate.isNotEmpty(baseIdList))andExprs.add(EntityCondition.makeCondition("groupId", EntityOperator.IN,baseIdList)); 
		if (andExprs.size() > 0) mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);
		
    	try {
    		List<GenericValue>	entityList = delegator.findList("SecurityGroup",mainCond, null, null, null, true);
		
    	List<Map<String,Object>> list = FastList.newInstance();
    	Map<String,Object> keyMap = FastMap.newInstance();
    	String headers = "编号,名称,备注,所属机构";
    	result.put("headers", headers);
    	
    		for (GenericValue entity : entityList) {
    			Map<String,Object> map = FastMap.newInstance();

    			
    			map.put("groupId", entity.getString("groupId"));
    			keyMap.put("0", "groupId");
    			
    			map.put("description", entity.getString("description"));
    			keyMap.put("1", "description");
    			
    			map.put("longDescription", entity.getString("longDescription"));
    			keyMap.put("2", "longDescription");
    			
    			map.put("ownerPartyId", entity.getString("ownerPartyId"));
    			keyMap.put("3", "ownerPartyId");
    			
    			
    			list.add(map);
    		}
    		result.put("list", list);
    		result.put("keyMap", keyMap);
    	} catch (GenericEntityException e) {
    		Debug.logError(e.getMessage(), module);
    	}
    	return result;
    }


	/**
	 * 导入安全组
	 */
	public static Map<String, Object> importCoSecurityGroup(DispatchContext dctx,
															 Map<String, ? extends Object> context) {
		Map<String, Object> result = ServiceUtil.returnSuccess();
		Delegator delegator = dctx.getDelegator();
		List<GenericValue> toBeStore = new ArrayList();
		List<String> messageList = new ArrayList<String>();
		List<Map<String,Object>> sheetList = (List<Map<String,Object>>) context.get("sheetList");
		
		LocalDispatcher dispatcher = dctx.getDispatcher();
		GenericValue userLogin = (GenericValue) context.get("userLogin");
		String partyId = userLogin.getString("partyId");
		Map service_context =UtilMisc.toMap("userLogin", userLogin,"partyId",partyId);
		String ownerPartyId="";
		try {
			Map service_result =dispatcher.runSync("getOwnerPartyId", service_context);
			ownerPartyId=(String) service_result.get("ownerPartyId");
		} catch (GenericServiceException e) {
			Debug.logError(e.getMessage(), module);
		}
		
		try {
			int i =0;
			for(Map<String,Object> row : sheetList){
				i=i+1;
				String groupId = ExcelPoiUtils.getCellString(row, 0);
				String description = ExcelPoiUtils.getCellString(row, 1);
				String longDescription = ExcelPoiUtils.getCellString(row, 2);
				//String ownerPartyId = ExcelPoiUtils.getCellString(row, 3);


				//判断
				if(UtilValidate.isEmpty(description)){
					messageList.add("第" + i + "行,权限组名称不能为空。" );
					continue;
				}


				//创建
				if(UtilValidate.isEmpty(groupId)){
					groupId =delegator.getNextSeqId("SecurityGroup");
				}

				GenericValue entity = delegator.makeValue("SecurityGroup",UtilMisc.toMap("groupId", groupId));
				entity.set("removed", "N");
				entity.set("description", description);
				entity.set("ownerPartyId", ownerPartyId);
				if(UtilValidate.isNotEmpty(longDescription)){
					entity.set("longDescription", longDescription);
				}
				
				toBeStore.add(entity);

			}
			delegator.storeAll(toBeStore);
			result.put("messageList", messageList);
		} catch (GenericEntityException e) {
			Debug.logError(e.getMessage(), module);
		}
		return result;
	}

	/**
	 * 导出安全组
	 */

	public static Map<String, Object> exportCoSecurityGroup(DispatchContext dctx,
															 Map<String, ? extends Object> context) {
//    	java.text.DateFormat orderDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Delegator delegator = dctx.getDelegator();
		String ids =  (String)context.get("ids");
		List<String> baseIdList = FastList.newInstance();
		if(UtilValidate.isNotEmpty(ids)){
			baseIdList = Arrays.asList(ids.split(";"));
		}
		Map<String, Object> result = ServiceUtil.returnSuccess();

		EntityCondition mainCond = null;
		List<EntityCondition> andExprs = FastList.newInstance();

		if(UtilValidate.isNotEmpty(baseIdList))andExprs.add(EntityCondition.makeCondition("groupId", EntityOperator.IN,baseIdList));
		if (andExprs.size() > 0) mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

		try {
			List<GenericValue>	entityList = delegator.findList("SecurityGroup",mainCond, null, null, null, true);

			List<Map<String,Object>> list = FastList.newInstance();
			Map<String,Object> keyMap = FastMap.newInstance();
			String headers = "编号,名称,备注";
			result.put("headers", headers);

			for (GenericValue entity : entityList) {
				Map<String,Object> map = FastMap.newInstance();


				map.put("groupId", entity.getString("groupId"));
				keyMap.put("0", "groupId");

				map.put("description", entity.getString("description"));
				keyMap.put("1", "description");

				map.put("longDescription", entity.getString("longDescription"));
				keyMap.put("2", "longDescription");

//				map.put("ownerPartyId", entity.getString("ownerPartyId"));
//				keyMap.put("3", "ownerPartyId");


				list.add(map);
			}
			result.put("list", list);
			result.put("keyMap", keyMap);
		} catch (GenericEntityException e) {
			Debug.logError(e.getMessage(), module);
		}
		return result;
	}
	
	
}
